//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.PayList;
import com.aidong.model.wills.PtContract;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PtContractDao extends BaseDao<PtContract, Integer> {
	public PtContractDao() {
	}

	public PtContract getByContractCode(String contractCode) {
		return (PtContract)this.getByWhere("contractCode=:p1", new Object[]{contractCode});
	}

	public List<PtContract> getListByMemberCode(String memberCode) {
		return this.queryList("memberCode=:p1", new Object[]{memberCode}, (new OrderSet()).asc("endTime"), -1);
	}

	public List<PtContract> getValidListByMemberCode(String memberCode, String shopCode) {
		return this.queryList("memberCode=:p1 and shopCode=:p2 and quantity>bespeakQuantity and endTime>=convert(varchar(10), getdate(),120) and outTime is null", new Object[]{memberCode, shopCode}, (new OrderSet()).asc("endTime"), -1);
	}

	public Date getMaxEndTimeByMemberCode(String memberCode) {
		String sql = "select max(endTime) from PtContract where memberCode=:p1 and outTime is null";
		return (Date)this.getBySql(Date.class, sql, new Object[]{memberCode});
	}

	public Integer getCountByMemberCode(String memberCode) {
		String sql = "select count(*) nn from PtContract where memberCode=:p1";
		return (Integer)this.getBySql(Integer.class, sql, new Object[]{memberCode});
	}

	public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.SaleId,c.ContractCode,c.ShopCode,c.MemberCode,c.EarnestId,c.PromotionId,c.SaleType,c.SaleDate,c.PtCode,c.CoachCode,c.Quantity,c.FactPrice,c.EarnestAmount,c.PayAmount,c.BeginTime,c.EndTime,c.BespeakQuantity,c.ClearQuantity,c.Note,c.Status,c.ApproveUser,c.ApproveTime,c.OutQuantity,c.OutAmount,c.OutType,c.OutTime,c.OldContractCode,c.commissionDate,c.SignStatus,c.PrintLabel,c.PrintTime,c.PrintUser,c.SalesCode,c.SalesCode2,c.createUser,c.receivePaper,c.reviewStatus,c.reviewDate,e.earnestDelay,i.chName ptCodeName,i.itemType,m.cardCode,m.chName memberName\nfrom PtContract c inner join PtItemCode i on c.ptCode=i.code\ninner join MemberInfo m on c.memberCode=m.memberCode\nleft join MemberEarnest e on c.earnestId=e.saleId\n" + (sqlw.length() > 0?" where " + sqlw:"") + " order by c.saleDate desc";
		this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
	}

	public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd, Date date1, Date date2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dt1 = sdf.format(date1);
		String dt2 = sdf.format(date2);
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.saleId,c.contractCode,c.shopCode,c.memberCode,m.chName memberName,m.cardCode,c.saleType,c.saleDate,c.beginTime,c.endTime\n,c.ptCode,c.coachCode,c.quantity,c.factPrice,c.bespeakQuantity,c.clearQuantity,c.outQuantity,c.outTime,c.outType\n,(select count(*) from PtBespeak where ContractCode=c.ContractCode and status=1 and ClearTime<\'" + dt1 + "\') quantity1\n" + ",(select count(*) from PtBespeak where ContractCode=c.ContractCode and status=1 and ClearTime>=\'" + dt1 + "\' and ClearTime<\'" + dt2 + "\') quantity2\n" + ",(case when c.saleDate>=\'" + dt1 + "\' and c.saleDate<\'" + dt2 + "\' then c.quantity else 0 end) quantity3\n" + ",(case when c.outTime>=\'" + dt1 + "\' and c.outTime<\'" + dt2 + "\' then c.outQuantity else 0 end) quantity4\n" + ",(select max(classTime) from PtBespeak where ContractCode=c.ContractCode and status=1) classTime\n" + "from PtContract c inner join MemberInfo m on c.MemberCode=m.MemberCode\n" + "inner join PtItemCode i on c.ptCode=i.code\n" + "where " + (sqlw.length() > 0?sqlw + " and ":"") + " c.saleDate<\'" + dt2 + "\' and (lastClearTime>=\'" + dt1 + "\' or OutTime>=\'" + dt1 + "\' or (quantity>ClearQuantity and OutTime is null))\n" + "order by c.saleId desc";
		this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
	}

	public void addContract(PtContract contract, List<PayList> payList) {
		String shopCode = contract.getShopCode();
		String sql = "select max(contractCode) from PtContract where shopCode=:p1 and contractCode like :p2";
		String maxCode = (String)this.getBySql(String.class, sql, new Object[]{shopCode, shopCode + "%"});
		int maxId = 0;
		if(maxCode != null && maxCode.length() > shopCode.length()) {
			try {
				maxId = Integer.parseInt(maxCode.substring(shopCode.length()));
			} catch (Exception var10) {
				;
			}
		}

		String contractCode = "000000" + (maxId + 1);
		contractCode = shopCode + contractCode.substring(contractCode.length() - 6);
		contract.setContractCode(contractCode);
		this.add(contract);
		Iterator memberName = payList.iterator();

		while(memberName.hasNext()) {
			PayList sign = (PayList)memberName.next();
			sign.setSaleId(contract.getSaleId());
			sign.setContractCode(contractCode);
			this.getSession().save(sign);
			if("S011".equalsIgnoreCase(sign.getPayType())) {
				sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
				this.updateBySql(sql, new Object[]{Integer.valueOf(2), sign.getAutoId(), sign.getSalesCode(), sign.getSalesCode2(), new Date(), sign.getNote()});
			}
		}

		if(contract.getEarnestId() != null) {
			sql = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
			this.updateBySql(sql, new Object[]{contract.getSaleId(), contract.getEarnestId()});
		}

		String memberName1 = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{contract.getMemberCode()});
		ContractSign sign1 = new ContractSign();
		sign1.setShopCode(contract.getShopCode());
		sign1.setContractCode(contract.getContractCode());
		sign1.setContractType("02");
		sign1.setSaleId(contract.getSaleId());
		sign1.setMemberCode(contract.getMemberCode());
		sign1.setMemberName(memberName1 == null?"":memberName1);
		sign1.setStatus(Integer.valueOf(1));
		sign1.setSalesCode(contract.getSalesCode());
		sign1.setSalesCode2(contract.getSalesCode2());
		sign1.setCreateUser(contract.getCreateUser());
		sign1.setCreateTime(new Date());
		sign1.setUpdateTime(new Date());
		this.getSession().save(sign1);
	}
}
